﻿using IR.WPF.UI.Controls;
using IR.WPF.UI.Windows;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IR.WPF.UI.ViewModels
{
    public class VMHome : ViewModelBase
    {
        private VMVersionHeader _versionHeader;
        private VMIdeaHeader _ideaHeader;
        private ICommand _logoutCommand;
        private ICommand _versionPanelCommand;
        private ICommand _userPanelCommand;
        private ICommand _ideaPanelCommand;

        public VMHome()
        {
            WindowTitle = "IdeaRepository";
        }

        #region **Properties**
        public VMVersionHeader HomeVersionHeader {
            get {
                if (_versionHeader == null)
                    _versionHeader = new VMVersionHeader();

                return _versionHeader;
            }
        }

        public VMIdeaHeader HomeIdeaHeader {
            get {
                if (_ideaHeader == null)
                    _ideaHeader = new VMIdeaHeader();

                return _ideaHeader;
            }
        }
        #endregion

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Loggingout
        /// </summary>
        public ICommand Logout
        {
            get
            {
                if (_logoutCommand == null)
                    _logoutCommand = new RelayCommand(p => LogoutFunction(p as Control), p => CanLogout(p as Control));

                return _logoutCommand;
            }
        }

        public ICommand VersionPanel
        {
            get
            {
                if (_versionPanelCommand == null)
                    _versionPanelCommand = new RelayCommand(p => VersionPanelFunction(p as Control), p => CanShowVersionPanel(p as Control));

                return _versionPanelCommand;
            }
        }

        public ICommand UserPanel
        {
            get
            {
                if (_userPanelCommand == null)
                    _userPanelCommand = new RelayCommand(p => UserPanelFunction(p as Control), p => CanShowUserPanel(p as Control));

                return _userPanelCommand;
            }
        }

        public ICommand IdeaPanel
        {
            get
            {
                if (_ideaPanelCommand == null)
                    _ideaPanelCommand = new RelayCommand(p => IdeaPanelFunction(p as Control), p => CanShowIdeaPanel(p as Control));

                return _ideaPanelCommand;
            }
        }
        #endregion

        #region **Command Functions**
        /// <summary>
        /// Called to logout the user
        /// </summary>
        /// <param name="sender">Reference of the Control that is triggering the action</param>
        protected void LogoutFunction(Control sender)
        {
            Button btn = sender as Button;
            Window.GetWindow(btn).Close();
        }

        protected void VersionPanelFunction(Control sender)
        {
            // Create a new window that displays the window
            VMViewPage viewModel = new VMViewPage();

            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            CompositeControlHost.ShowModal<VersionsView>(viewModel, window);
        }

        protected void UserPanelFunction(Control sender)
        {
            // Create a new window that displays the window
            VMViewPage viewModel = new VMViewPage();

            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            CompositeControlHost.ShowModal<UsersView>(viewModel, window);
        }

        protected void IdeaPanelFunction(Control sender)
        {
            // Create a new window that displays the window
            VMViewPage viewModel = new VMViewPage();

            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            CompositeControlHost.ShowModal<IdeasView>(viewModel, window);
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanLogout(Control sender)
        {
            return true;
        }

        protected bool CanShowVersionPanel(Control sender)
        {
            return true;
        }

        protected bool CanShowIdeaPanel(Control sender)
        {
            return true;
        }

        protected bool CanShowUserPanel(Control sender)
        {
            return true;
        }
        #endregion
    }
}
